package cn.edu.scau.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
/**
 * 自定义日期转换器Converter<String, Date>
 * @author ZERO
 *
 */
@Component
public class MyDateConvert implements Converter<String, Date>{

	@Override
	public Date convert(String source) {
		SimpleDateFormat sdf = null;
		if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
		Date date = null;
		try {
			date = sdf.parse(source);
		}catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
}
